import moment from "moment"

const add=function(data,_type){
    let key='UT-'+localStorage.getItem('userId')+'-BRDS';
    let items=localStorage.getItem(key);
    if(!items){
        items=[];
    }else{
        items=JSON.parse(decodeURIComponent(items));
    }
    data.favdate=moment().format('YYYY-MM-DD HH:mm:ss');
    data.smfavdate=moment().format('YYYY-MM-DD');
    // data.favdate=moment().format('2024-10-18 16:54:22');
    // data.smfavdate=moment().format('2024-10-18');

    data.favmodule=_type;

    items=items.filter(item=>{
        return item.id!=data.id;
    })

    let newObj={
        id:data.id,
        updateTime:data.updateTime,
        createTime:data.createTime,
        create_time:data.create_time,
        favmodule:_type,
        favdate:moment().format('YYYY-MM-DD HH:mm:ss'),
        smfavdate:moment().format('YYYY-MM-DD')
    }
    items.push(newObj);

    localStorage.setItem(key,encodeURIComponent(JSON.stringify(items)));
}

const filterList=function(list, key) {
    let obj = {};
    list.forEach(item=>{
        obj[item[key]]=item;
    });
    return Object.values(obj);
}

const get=function(){
    let key='UT-'+localStorage.getItem('userId')+'-BRDS';
    let items=localStorage.getItem(key);
    if(!items){
        items=[];
    }else{
        items=JSON.parse(decodeURIComponent(items));
    }
    let newDatas=[];
    let newItems=filterList(items,'smfavdate');

    let dates=newItems.map(r=>{
        return r.smfavdate;
    })

    dates.reverse();
    
    dates.map(date=>{
        let _list=items.filter(r=>{
            return r.smfavdate==date;
        })
        newDatas.push({daystr:date,list:_list,smfdatestr:moment(date).format('MM月DD日')})
    })
    return newDatas;
}

const del=function(_id){
    let key='UT-'+localStorage.getItem('userId')+'-BRDS';
    let items=localStorage.getItem(key);
    if(!items){
        items=[];
    }else{
        items=JSON.parse(items);
    }
    let newItems=items.filter(r=>{
        return r.id!=_id;
    })
    localStorage.setItem('bds',JSON.stringify(newItems));
}

const addCirRecord=function(_module){
    let key='UT-'+localStorage.getItem('userId')+'-CIR-'+_module;
    let moduleRecord=localStorage.getItem(key);
    if(moduleRecord){
        moduleRecord=parseInt(moduleRecord)+1;
        localStorage.setItem(key,moduleRecord);
    }else{
        localStorage.setItem(key,1);
    }
}

const clearall=function(_id){
    let key='UT-'+localStorage.getItem('userId')+'-BRDS';
    localStorage.removeItem(key);
}

const checkLogin=function(_this,_isTake){
    let logind=localStorage.getItem('token')&&localStorage.getItem('userId');
    if(!logind){
        let parent=_this.$parent;
        if(!parent.isAppMain){
            parent=parent.$parent;
        }
        if(!parent.isAppMain){
            parent=parent.$parent;
        }
        if(!parent.isAppMain){
            parent=parent.$parent;
        }
        if(!parent.isAppMain){
            parent=parent.$parent;
        }
        if(!parent.isAppMain){
            parent=parent.$parent;
        }
        if(parent.isAppMain){
            parent.noLoginIsTak = _isTake?_isTake:false;
            parent.showLoginView = true;
        }
        return false;
    }
    return true;
}

const convertImgItem=function(item,that){
    item.collect=item.collect==undefined?false:item.collect;
    item.praise=item.praise==undefined?false:item.praise;
    item.praise_count=item.praise_count==undefined?0:item.praise_count;

    item.selected = false;

    item.thumbnailBean = {
        id: item.id,
        thumbnail: item.content,
        imageWidth: item.width,
        imageHeight: item.height
    }

    if (item.tags) {
        item.tags = item.tags.split(',');
    } else {
        item.tags = [];
    }
    let _vtime = null;
    if (item.update_time) {
        _vtime = item.update_time;
    } else if (item.create_time) {
        _vtime = item.create_time.replace('T', ' ');
    }
    _vtime = Math.abs(new Date(_vtime).getTime());
    item.lazy = {
        src: that.$imgdomainurl + item.id + '-' + _vtime + '@1x560.webp',
        error: require('@/assets/l_error_'+(localStorage.getItem('THM-STY')?localStorage.getItem('THM-STY'):'light')+'.png'),
        loading: require('@/assets/l_error_'+(localStorage.getItem('THM-STY')?localStorage.getItem('THM-STY'):'light')+'.png')
    }
    let srcsets = [];
    srcsets.push(that.$imgdomainurl + item.id + '-' + _vtime + '@1x560.webp 1x');
    srcsets.push(that.$imgdomainurl + item.id + '-' + _vtime + '@2x560.webp 2x');
    srcsets.push(that.$imgdomainurl + item.id + '-' + _vtime + '@3x560.webp 3x');
    item.srcsets = srcsets.join(',')

    item.w = item.thumbnailBean.imageWidth;
    item.h = item.thumbnailBean.imageHeight;
    item.sw = item.w;
    item.sh = item.h;


    item.ratios = [];
    if (item.text) {
        item.text.map(text => {
            let cor = text.coordinate;

            let corners=[
            {x:cor[0][0],y:cor[0][1]},
            {x:cor[1][0],y:cor[1][1]},
            {x:cor[2][0],y:cor[2][1]},
            {x:cor[3][0],y:cor[3][1]}
            ]
            let angle=that.calculateTilt(corners);

            text.width = cor[2][0] - cor[0][0];
            text.height = cor[2][1] - cor[0][1];

            text.centerX = cor[0][0];
            text.centerY = cor[0][1];

            item.ratios.push({
                angle:angle,
                rw:(text.width / item.w) * 100,
                rleft:(text.centerX / item.w) * 100,
                rh:(text.height / item.h)* 100,
                rtop:(text.centerY / item.h) * 100
                // rh:angle<0?(text.height / item.h)* 100+5:(text.height / item.h)* 100,
                // rtop:angle<0?(text.centerY / item.h) * 100-3:(text.centerY / item.h) * 100
            })
        })
    }
}

const vlist=[{
    sw: 433,show:false,showOther:false,
    sh: 350
    },{
    sw: 433,show:false,showOther:false,
    sh: 600
    },{
    sw: 433,show:false,showOther:false,
    sh: 280
    },{
    sw: 433,show:false,showOther:false,
    sh: 600
    },{
    sw: 433,show:false,showOther:false,
    sh: 350
    },{
    sw: 433,show:false,showOther:false,
    sh: 350
    },{
    sw: 433,show:false,showOther:false,
    sh: 350
    },{
    sw: 433,show:false,showOther:false,
    sh: 600
    },{
    sw: 433,show:false,showOther:false,
    sh: 280
    },{
    sw: 433,show:false,showOther:false,
    sh: 600
    },{
    sw: 433,show:false,showOther:false,
    sh: 350
    },{
    sw: 433,show:false,showOther:false,
    sh: 350
    },{
    sw: 433,show:false,showOther:false,
    sh: 350
    }
];

let faceDomain='https://www.gameui.net'

//表情转换
const replace_em=function(str) {
    
    str = str.replace(/\</g, '&lt;');

    str = str.replace(/\>/g, '&gt;');

    str = str.replace(/\n/g, '<br/>');

    str = str.replace(/\[em_([0-9]*)\]/g, "<img src='"+faceDomain+"/emoji/$1.svg?" + Date.parse(new Date()) + "' />");

    str = str.replace(/\[call_em_([0-9]*)\]/g, "<img src='"+faceDomain+"/emoji/$1.svg?" + Date.parse(new Date()) + "' />");

    return str;
}

export default{
    add,get,del,clearall,replace_em,checkLogin,addCirRecord,convertImgItem,vlist
}